/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Hops;
import com.openbrew.openbrewrf.Malt;
import com.openbrew.openbrewrf.POLines;
import com.openbrew.openbrewrf.Supplies;
import com.openbrew.openbrewrf.Yeast;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.enumerations.WarehouseIngredient;
import tools.InventoryModifiers;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.polines")
public class POLinesFacadeREST extends AbstractFacade<POLines> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public POLinesFacadeREST() {
        super(POLines.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public POLines create(@WebParam(name = "polines") POLines entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(POLines entity) {
        super.edit(entity);
    }

    @PUT
    @Path("editAndUpdateInventories")
    @Consumes({"application/xml", "application/json"})
    public void editAndUpdateInventory(POLines entity) {
        final boolean newVal = entity.getPOLRcvd();
        POLines poLine = getPOLinesFromID(entity.getPolid(), em);
        if((poLine.getPOLRcvd()==false) && (newVal == true)){
            modifyInventory(entity, entity.getpOLQtyRcvd());
        } else if((poLine.getPOLRcvd()==true) && (newVal == false)){
            modifyInventory(entity, (-1 * entity.getpOLQtyRcvd()));
        }
        super.edit(entity);
    }
    
    private POLines getPOLinesFromID(final int id, final EntityManager en){
        TypedQuery<POLines> query = en.createQuery(
                    "SELECT p FROM POLines p WHERE p.polid = ?1", POLines.class);
        query.setParameter(1, id);
        final POLines poLine = query.getSingleResult();
        return poLine;
    }
    
    private void modifyInventory(final POLines entity, final int quantity) {
        final InventoryModifiers im = new InventoryModifiers();
        final int tid = entity.getPolpn().getTableID();
        final int whID = entity.getPolpn().getWhid().getWearhouseID();
        if(whID == WarehouseIngredient.HOPS.getDbId()){
            im.doReceiveHops(em, tid, quantity);
        } else if(whID == WarehouseIngredient.MALT_AND_ADJUNCTS.getDbId()){
            im.doReceiveMalt(em, tid, quantity);
        } else if(whID != WarehouseIngredient.YEAST.getDbId()){
            im.doReceiveSupply(em, tid, quantity);
        }
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public POLines find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<POLines> findAll() {
        final List<POLines> poLines = super.findAll();
        for(final POLines poLine : poLines){
            final int whID = poLine.getPolpn().getWhid().getWearhouseID();
            final int tid = poLine.getPolpn().getTableID();
            if(whID == WarehouseIngredient.HOPS.getDbId()){
                final Hops hop = getHopFromID(tid);
                poLine.setDescription(hop.getHopName());
            } else if(whID == WarehouseIngredient.MALT_AND_ADJUNCTS.getDbId()){
                final Malt malt = getMaltFromID(tid);
                poLine.setDescription(malt.getMaltName());
            } else if(whID == WarehouseIngredient.YEAST.getDbId()){
                final Yeast yeast = getYeastFromID(tid);
                poLine.setDescription(yeast.getYeastDesc());
            } else {
                final Supplies supply = getSupplyFromID(tid);
                poLine.setDescription(supply.getSupName());
            }
        }
        return poLines;
    }
    
    private Hops getHopFromID(final int id){
        TypedQuery<Hops> query = em.createQuery(
                    "SELECT h FROM Hops h WHERE h.hid = ?1", Hops.class);
        query.setParameter(1, id);
        return query.getSingleResult();
    }
    
    private Malt getMaltFromID(final int id){
        TypedQuery<Malt> query = em.createQuery(
                    "SELECT m FROM Malt m WHERE m.maltID = ?1", Malt.class);
        query.setParameter(1, id);
        return query.getSingleResult();
    }
    
    private Yeast getYeastFromID(final int id){
        TypedQuery<Yeast> query = em.createQuery(
                    "SELECT y FROM Yeast y WHERE y.yid = ?1", Yeast.class);
        query.setParameter(1, id);
        return query.getSingleResult();
    }
    
    private Supplies getSupplyFromID(final int id){
        TypedQuery<Supplies> query = em.createQuery(
                    "SELECT s FROM Supplies s WHERE s.supID = ?1", Supplies.class);
        query.setParameter(1, id);
        return query.getSingleResult();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<POLines> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
